package com.eliansun.crawler.engine;

import com.alibaba.fastjson.JSONObject;
import com.eliansun.crawler.downloader.RequestsDownLoad;
import com.eliansun.crawler.downloader.defaultdowloader.ProxyHttpDownload;
import com.eliansun.crawler.downloader.phantomjsdriver.PhantomjsDownloader;
import com.eliansun.crawler.pageparse.AbstractPageProcess;
import com.eliansun.crawler.pipeline.AbstractPipeline;
import com.eliansun.crawler.spider.MySpider;
import com.eliansun.service.mongo.IMongoWapper;
import com.eliansun.util.BaseUtil;
import com.eliansun.util.DatePattern;
import org.apache.log4j.Logger;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Encoder;
import us.codecraft.webmagic.Request;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author xpq
 * <p>
 * task main
 */
@Component
public class GetTaskForSpider implements Runnable {

    String batch = BaseUtil.getDateTimeStr(DatePattern.YM);

    @Autowired
    private IMongoWapper mongoWapper;

    private Logger logger = Logger.getLogger(getClass());

    @Value("${system.spider.id}")
    private String spiderId;

    @Autowired
    private AbstractPipeline myPipeline;


    @Override
    public void run() {
        mongoWapper.setCollectionName("sysSpider");
        int count = new Long(mongoWapper.size()).intValue();
        if (count == 0) {
            logger.info("当前没有需执行的任务！");
            return;
        }
        logger.info(Thread.currentThread().getName() + "任务开始");


        while (count != 0) {
            Document document = mongoWapper.selectOneAndDelete(new Document("spiderId", 0));
            if (document != null) {
                String keyWords = String.valueOf(document.get("keyWord"));
                String packageName = String.valueOf(document.get("packageName")).trim();
                String searchUrl = String.valueOf(document.get("searchUrl")).trim();
                String cTerrace = String.valueOf(document.get("terrace")).trim();
                String provinceName = document.get("provinceName").toString();
                String provinceId = document.get("provinceId").toString();
                String mainUrl = String.valueOf(document.get("url"));
                int urlcode = Integer.valueOf(String.valueOf(document.get("urlCode")));
                int isLazy = Integer.valueOf(String.valueOf(document.get("lazy")).trim());
                String cBatch = String.valueOf(document.get("batch"));
                String spiderJobId = String.valueOf(document.get("spiderJobId"));
                try {
                    Class clazz = Class.forName(packageName);
                    AbstractPageProcess pageProcessor = (AbstractPageProcess) clazz.newInstance(); //反射得到对应的解析器
                    pageProcessor.setMain_Url(mainUrl);
                    pageProcessor.setWordKey(keyWords);
                    pageProcessor.setcTerrace(cTerrace);
                    pageProcessor.setProvinceName(provinceName);

                    MySpider spider = new MySpider();
                    //set 任务采集批次号
                    Map<String, Object> UUID = new HashMap<>(2);
                    UUID.put("provinceId", provinceId);
                    UUID.put("produceArea", provinceName);
                    UUID.put("spiderJobId", spiderJobId);
                    UUID.put("terrace", cTerrace);
                    UUID.put("keyWord", keyWords);
                    UUID.put("batch", cBatch);
                    UUID.put("mainUrl", mainUrl);
                    spider.setUUID(JSONObject.toJSONString(UUID));
                    pageProcessor.setMySpider(spider);
                    spider.setPageProcessor(pageProcessor);
                    String targetUrl = searchUrl;
                    if (!keyWords.equals("")) {
                        String[] pattern = {"(", ")"};
                        for (int i = 0; i < pattern.length; i++) {
                            if (keyWords.indexOf(pattern[i]) != -1) {
                                urlcode = 3;
                            }
                        }
                    }
                    switch (urlcode) {
                        case 0:
                            targetUrl = searchUrl.replace("@key@", URLEncoder.encode(keyWords, "UTF-8"));
                            break;
                        case 1:
                            targetUrl = searchUrl.replace("@key@", URLEncoder.encode(keyWords, "gb2312"));
                            break;
                        case 2:
                            targetUrl = searchUrl.replace("@key@", URLEncoder.encode(URLEncoder.encode(keyWords, "UTF-8"), "UTF-8"));
                            break;
                        case 3:
                            targetUrl = searchUrl.replace("@key@", URLDecoder.decode(URLEncoder.encode(keyWords, "gb2312"), "UTF-8"));
                            break;
                    }
                    Request request = new Request(targetUrl);
                    spider.addRequest(request);
                    spider.addPipeline(myPipeline);
                    if (isLazy == 1) {
                        spider.setDownloader(new PhantomjsDownloader());
                    }
                    if (isLazy == 2) {
                        spider.setDownloader(new ProxyHttpDownload());
                    }
                    if (isLazy == 3) {
                        spider.setDownloader(new RequestsDownLoad(mainUrl));
                    }
                    spider.thread(5);
                    spider.run();
                    count = new Long(mongoWapper.size()).intValue();
                } catch (Exception e) {
                    logger.error(e);
                    if (document != null) {
                        mongoWapper.save(document); //模拟回滚
                    }
                    continue;
                }
            }
        }
        logger.info(Thread.currentThread().getName() + "任务执行完毕");
    }


}
